<?php
session_start();
//销售员，列出所选项目的报价汇总和技术标书
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>


<br />
<br />
<p class="text-center lead">提交文件列表</p>


<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');

$project_code = $_GET['project_code'];
$db= db_connect();

//下载文件
if (isset($_GET['file_name'])) {
	$file_name = $_GET['file_name'];
	$file_id = $_GET['file_id'];
	require_once('../FNS/ftp_class2.php');
	$target_name = "d:\\".$file_name;
	$ftp_config = array(
		'hostname' => 'localhost',
		'username' => 'bid',
		'password' => '121121',
		'port' => 21
	);

	$ftp = new Ftp();
	$ftp->connect($ftp_config);
	
	$ftp->download($file_name,$target_name);
	
	$ftp->close();
	
	echo "<p>文件已下载至D盘根目录下。</p>";
	//echo "<p>文件id".$file_id."。</p>";
	$get_date = date ('Y-m-d');
	
	$query = "select check_result from files where id = '".$file_id."'";
	$result=$db->query ($query);
	$row=$result->fetch_assoc ();
	$check_result = stripslashes ($row['check_result']);
	$result->free();
	
	if (!$check_result) {
	//如果没有下载记录则记录下载时间
	$query = "update files set checker = '".$user_id."', check_date = '".$get_date."', check_result = '接收' where id = '".$file_id."'";
	$result=$db->query ($query);
	//echo "<p>文件已记录。</p>";
	}

}

	//把这个项目的所有文件搜出来
	//还需要获取提交人
	$query="select files.type, files.id, files.name, files.issue_date, files.rev, files.check_date, files.check_result, files.issuer, a.name as issue_name, b.name as check_name from files 
		left join users as a on files.issuer = a.id 
		left join users as b on files.checker = b.id
		where files.name LIKE '".$project_code."%' and files.subject = 'pr'";

//连接数据库并搜索数据
$result=$db->query ($query);
if (!$result) {
	echo "<p>项目编号：".$project_code."。无提交记录。</p>";
	echo "</div>";
	echo "</body>";
	echo "</html>";
	exit;
}
//获取记录数量
$num_results=$result->num_rows;
//echo "<p>记录条数是：".$num_results."</p>";
//写表头
echo "<table class='table table-striped'>";
echo "<tr>";
	echo "<td align='center' class='info'><strong>文件名</strong></td>";
	echo "<td align='center' class='info'><strong>版本号</strong></td>";
	echo "<td align='center' class='info'><strong>提出人</strong></td>";
	echo "<td align='center' class='info'><strong>提出时间</strong></td>";
	echo "<td align='center' class='info'><strong>接收人</strong></td>";
	echo "<td align='center' class='info'><strong>接收时间</strong></td>";
	echo "<td align='center' class='info'><strong>状态</strong></td>";
	echo "<td align='center' class='info'><strong>操作</strong></td>";
echo "</tr>";



//循环编制表格
for ($i=0; $i < $num_results; $i++) {

	$row=$result->fetch_assoc ();
	
	
	$files_id = stripslashes ($row['id']);
	$name = stripslashes ($row['name']);
	$rev = stripslashes ($row['rev']);
	$issuer = stripslashes ($row['issue_name']);
	$issuer_id = stripslashes ($row['issuer']);
	$checker = stripslashes ($row['check_name']);
	$type = stripslashes ($row['type']);
	//$checker = "结构专业负责人";
	$issue_date = stripslashes ($row['issue_date']);
	$check_date = stripslashes ($row['check_date']);
	$check_result = stripslashes ($row['check_result']);
	
	if ($type == "报价") {
	$download_link = "../PHP/list_proposal.php?project_code=".$project_code."&file_id=".$files_id."&file_name=".$name."_Rev.".$rev."_".$issuer_id.".xlsx";
	}
	if ($type == "标书") {
	$download_link = "../PHP/list_proposal.php?project_code=".$project_code."&file_id=".$files_id."&file_name=".$name."_Rev.".$rev."_".$issuer_id.".pdf";
	}
	
	echo "<tr>";
    echo "<td align ='center'>".$name."</td>";
	echo "<td align ='center'>".$rev."</td>";
	echo "<td align ='center'>".$issuer."</td>";
	echo "<td align ='center'>".$issue_date."</td>";
	echo "<td align ='center'>".$checker."</td>";
	echo "<td align ='center'>".$check_date."</td>";
	echo "<td align ='center'>".$check_result."</td>";
	echo "<td align='center'>
		<a class='btn btn-success btn-sm' href='".$download_link."' role='button'>下载</a> 
		</td>";
	echo "</tr>";
	
}
echo "</table>";
$result->free();
$db->close();



?>

</div>
</body>
</html>
